package cn.jg.framework.common.exception;

import cn.jg.framework.auth.model.Company;
import cn.jg.framework.common.util.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;

import java.util.Collection;
import java.util.Locale;

public class I18NUtil {

    private static MessageSource messageSource = SpringContextHolder.getBean(MessageSource.class);

    public static String getI18NMessage(String code, Object[] arguments) {
        if (code==null) return StringUtils.EMPTY;

        Locale currentLocale = getLocale();
        return messageSource.getMessage(code,arguments, currentLocale);
    }

    public static String getI18NMessage(String code, Collection arguments) {
        return getI18NMessage(code, arguments == null ? new Object[0] : arguments.toArray());
    }

    public static String getMessage(String code, Object... messages) {
        return getI18NMessage(code, messages);
    }

    public static String getI18NMessage(String code) {
        try {
            Locale currentLocale = getLocale();
            return messageSource.getMessage(code, null, currentLocale);
        } catch (Exception e) {
            return code;
        }
    }

    public static Locale getLocale() {
        /*try {
           // return SessionManager.getLocale();
            return null;
        } catch (Exception e) {
            return Locale.SIMPLIFIED_CHINESE;
        }*/
        return Locale.SIMPLIFIED_CHINESE;
    }

    public static String getI18NMessage(String code,Locale currentLocale) {
        return messageSource.getMessage(code,null,currentLocale);
    }

    public static String getI18NMessage(String code,Locale locale,Object... arguments){
        return messageSource.getMessage(code,arguments,locale);
    }

    public static String  getI18NMessage(String code,String language,Object[] arguments){
        Locale locale = Company.Language.getLocaleByValue(language);
        return messageSource.getMessage(code,arguments,locale);
    }

    public static String  getI18NMessage(String code,String language){
        return getI18NMessage(code,language,null);
    }

    public static boolean isSimpleChinese(){
       // return SessionManager.getLocale().equals(Locale.SIMPLIFIED_CHINESE);
        return true;
    }

    private I18NUtil() {
        throw new IllegalStateException("Utility class");
    }
}
